<?php
	defined('RUN') or die('No access');

	class ProductController
	{
		public function type($category_id, $page=1)
		{
			$data = Model::get('website')->getLayoutData();
			
			$product = Model::get('product')->getProductLimit('*', $category_id, $page, 5);
            foreach($product as $row){
                $price_sale = ($row['price']!=0 && $row['per']!=0)?((100-$row['per'])*$row['price']/100):$row['price'];
                $data['product'][] = array(
                    'id'        => $row['id'],
                    'name'      => $row['name'],
                    'content_short' => $row['content_short'],
                    'images'    => $row['images'],
                    'price'     => $row['price'],
                    'per'       => $row['per'],
                    'company'   => $row['company'],
                    'address'   => $row['address'],
                    'phone'     => $row['phone'],
                    'date_start'=> $row['date_start'],
                    'date_end'  => $row['date_end'],
                    'view'  => $row['view'],
                    'price_sale'=> number_format($price_sale,0,".",","),
                );
            }
			
			// Pagination
			$total_news = Model::get('product')->totalRows($category_id);
			Pagination::set('product', $total_news, 9, 3, $page);
			$data['pagination'] = Pagination::get('product');
			
            $category = Model::get('product_category')->getById($category_id);
			
			$data['title'] = ($category ? $category['name'] : Config::get('page_name'));
			$data['description'] = ($category ? $category['description'] : Config::get('page_description'));
			$data['keywords'] = ($category ? $category['keywords'] : Config::get('page_keywords'));
			$data['content_file'] = 'product/type.tpl';
			View::load('layout.tpl', $data, $data['temp']);
		}
        
        public function view($product_id){
            $data = Model::get('website')->getLayoutData();
			
			$data['product'] = Model::get('product')->getById($product_id,"*");
            $price_sale = ($data['product']['price']!=0 && $data['product']['per']!=0)?((100-$data['product']['per'])*$data['product']['price']/100):$data['product']['price'];
            $data['product']['price_sale'] = number_format($price_sale,0,".",",");
            
            
            $product = array('view'=> $data['product']['view']+1,);
            Model::get('product')->update($product, $product_id);
            
            
            $data['title'] = $data['product']['name'];
			$data['description'] = $data['product']['keywords'];
			$data['keywords'] = $data['product']['description'];
			$data['content_file'] = 'product/view.tpl';
			View::load('layout.tpl', $data, $data['temp']);
        }
	}
